package org.seckill.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class WebAppListener implements ServletContextListener, HttpSessionListener{

	private ServletContext application;
	
	private long userNumber = 0;
	
	public void contextDestroyed(ServletContextEvent event) {
		
	}

	public void contextInitialized(ServletContextEvent event) {
		application = event.getServletContext();
		application.setAttribute("root", application.getContextPath());
	}

	public void sessionCreated(HttpSessionEvent event) {
		userNumber++;
		application.setAttribute("userNumber", userNumber);		
	}

	public void sessionDestroyed(HttpSessionEvent event) {
		userNumber--;
		application.setAttribute("userNumber", userNumber);
	}

}
